<template>
  <el-form ref="formRef" :model="formModel" :rules="rules" label-width="130px" class="form" label-position="right" status-icon>
    <el-form-item label="调度员类型:" prop="type">
      <query-dict
        :value.sync="formModel.type"
        :dict-list="dispatcherType"
        :disabled="!hasRootRole"
        filterable
        class="form-select"
        placeholder="请选择类型"
      ></query-dict>
    </el-form-item>
    <el-form-item label="调度员姓名:" prop="name">
      <el-input v-model.trim="formModel.name" class="form-item" placeholder="请输入调度员姓名" clearable />
    </el-form-item>
    <el-form-item label="调度员手机号:" prop="phone">
      <el-input v-model.trim="formModel.phone" class="form-item" :disabled="isEdit" placeholder="请输入手机号" clearable />
    </el-form-item>
    <el-form-item label="调度员状态:" prop="status">
      <query-dict :value.sync="formModel.status" :dict-list="disabledStatus" filterable class="form-select" placeholder="请选择状态"></query-dict>
    </el-form-item>
  </el-form>
</template>

<script>
import dictObj from '@/store/dictData'
import MixinForm from 'mixins/form'
import { root } from '@/utils/constants'

export default {
  mixins: [MixinForm],
  props: {
    initData: {
      type: Object,
      default() {
        return {
          type: 2
        }
      }
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      disabledStatus: dictObj.disabledStatus,
      dispatcherType: dictObj.dispatcherType,
      rules: {
        type: [{ required: true, message: '请选择类型', trigger: 'change' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      }
    }
  },
  computed: {
    formModel: {
      get() {
        return this.initData
      },
      set(val) {
        this.$emit('update:init-data', val)
      }
    },
    hasRootRole() {
      const roles = this.$store.getters.roles
      if (roles) {
        return roles.some(role => role.val === root.rval)
      }
      return false
    }
  }
}
</script>

<style lang="less" scoped>
.form {
  margin-right: 50px;
  .form-item {
    width: 100%;
  }
  .form-select {
    width: 50%;
  }
}
</style>
